package com.plian.system.controller.sys.fun;

import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.config.Resubmit;
import com.plian.system.dto.sys.fun.SysFunDTO;
import com.plian.system.entity.sys.fun.SysFun;
import com.plian.system.model.CommonCode;
import com.plian.system.model.R;
import com.plian.system.service.sys.fun.ISysFunService;
import com.plian.system.vo.sys.fun.SysFunVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 账号-单位权限
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/sysfun")
@Api(value = "SysFunController", tags = "菜单 控制器")
public class SysFunController extends BaseController {

    private final ISysFunService sysFunService;

    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入 sysFun")
    public R<SysFunVO> detail(@RequestBody SysFun sysFun) {
        return R.data(sysFunService.detail(sysFun));
    }

    @PostMapping("/submit")
    @ApiOperation(value = "新增或修改", notes = "传入 sysFunVO")
    @Resubmit(delaySeconds = 0)
    public R submit(@Valid @RequestBody SysFunVO sysFunVO) {
        return sysFunService.submit(sysFunVO);
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除", notes = "传入 ids")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return sysFunService.removeBypartentId(ids)
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
//        return sysFunService.removeByIds(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
//                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    @PostMapping("/listForTree")
    @ApiOperation(value = "树状返回", notes = "传入 dto")
    public R<List<SysFunVO>> listForTree(@RequestBody SysFunDTO dto) {
        return R.data(sysFunService.tree(dto));
    }

    @PostMapping("/submit-list")
    @ApiOperation(value = "批量-新增或修改", notes = "传入 List<SysFun>")
    public R saveList( @RequestBody List<SysFun> sysFuns) {
        return sysFunService.saveList(sysFuns);
    }

}
